package com.web.main.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.entity.Admin;
import com.common.entity.Code;
import com.common.entity.ResponseResult;
import com.common.utils.JwtUtil;
import com.web.main.entity.form.AdminLogin;
import com.web.main.entity.form.AdminModify;
import com.web.main.entity.param.AdminQuery;
import com.web.main.entity.form.AdminRegister;
import com.web.main.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 长顺
 * @since 2020-11-21
 */
@Api(tags = "管理员模块-tags")
@RestController
@RequestMapping("/admin")
@Validated
public class AdminController {
    @Autowired
    AdminService  adminService;

    @ApiOperation(value = "注册管理员")
    @PostMapping("/add")
    public ResponseResult add(
            @ApiParam(name = "admin", value = "管理员对象", required = true)
            @Valid AdminRegister voAdmin) {
        boolean flag = adminService.register(voAdmin);
        if(flag) {
            return ResponseResult.success(Code.REGISTE_SUCCESS);
        } else {
            return ResponseResult.error(Code.REGISTER_ERROR);
        }
    }

    @ApiOperation(value = "管理员登录")
    @GetMapping("/login/")
    public ResponseResult login(
            @ApiParam(name = "username", value = "管理员账号", type = "path", required = true)
            @Valid AdminLogin adminLogin) {
        String username = adminLogin.getUsername();
        String password = adminLogin.getPassword();
        boolean flag = adminService.login(username, password);

        if(flag) {
            //登录成功，服务器生产token，响应客户端
            String token = JwtUtil.create(username);
            return ResponseResult.success(Code.LOGIN_SUCCESS).Data("token", token);
        }
        return ResponseResult.error(Code.LOGIN_ERROR);
    }

    @ApiOperation(value = "通过id查管理员")
    @GetMapping("/{adminId}")
    public ResponseResult getAdminById(
            @ApiParam(name = "adminId", value = "管理员主键" , required = true)
            @PathVariable String adminId) {
        Admin admin = adminService.getById(adminId);
        if(!StringUtils.isEmpty(admin)) {
            return ResponseResult.success(Code.REQUEST_SUCCESS).Data("admin", admin);
        }
        return ResponseResult.error(Code.REQUEST_ERROR);
    }

    @ApiOperation(value = "分页查询所有的管理员")
    @GetMapping("/page/{page}/{limit}")
    public ResponseResult getAll(
            @ApiParam(name = "page", value = "当前页", required = true)
            @PathVariable Integer page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Integer limit,
            @ApiParam(name = "adminQuery", value = "查询对象", required = false)
            @Valid AdminQuery adminQuery) {
        //创建分页对象
        Page<Admin> adminPage = new Page<>(page, limit);
        //调用service查询
        adminService.pageQuery(adminPage, adminQuery);
        //获取当前页数据
        List<Admin> records = adminPage.getRecords();
        //统一返回
        return ResponseResult.success(Code.REQUEST_SUCCESS).Data("rows", records);
    }

    @ApiOperation(value = "删除管理员")
    @DeleteMapping("/delete/{adminId}")
    public ResponseResult deleteById(
            @ApiParam(name = "token", value = "token", type = "String", required = false)
            @RequestHeader("token") String token,
//            @ApiParam(name = "httpRequest", value = "request域", hidden = true)
//            HttpRequest httpRequest,
            @ApiParam(name = "adminId", value = "管理员主键id", required = true)
            @PathVariable String adminId) {
        boolean flag = adminService.deleteAdmin(adminId, token);
        if(flag) {
            return ResponseResult.success(Code.REQUEST_SUCCESS);
        } else {
            return ResponseResult.error(Code.REQUEST_ERROR);
        }
    }


    @ApiOperation(value = "修改管理员信息")
    @PutMapping("/modify")
    public ResponseResult modifyAdminRole(
            @ApiParam(name = "token", value = "token", type = "String", required = true)
            @RequestHeader("token") String token,
            @ApiParam(name = "voAdminModify", value = "被修改对象", required = true)
            @Valid AdminModify voAdminModify) {

        boolean flag = adminService.modifyAdmin(voAdminModify, token);
        if (flag) {
            return ResponseResult.success(Code.REQUEST_SUCCESS);
        } else {
            return ResponseResult.success(Code.REQUEST_ERROR);
        }
    }

}

